﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace FisheryPlatform.Search.Core.Analysis
{
    /// <summary>
    /// HTML过滤器
    /// </summary>
    public class HtmlFilter : Filter
    {
        public HtmlFilter()
            : base("", "")
        {
        }

        protected internal override void LoadFilter()
        {
            // do nothing;
        }

        private string ReplaceHtml(Match m)
        {
            return string.Empty.PadLeft(m.Value.Length, ' ');
        }

        /// <summary>
        /// 重载Execute,过滤html的特殊字符
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public override string Execute(string key)
        {
            key = Regex.Replace(key, @"(<[/\w\d\s^>]*>)", new MatchEvaluator(this.ReplaceHtml), RegexOptions.IgnoreCase | RegexOptions.Singleline);
            key = Regex.Replace(key, @"(&[#\w\d\s]*;)", new MatchEvaluator(this.ReplaceHtml), RegexOptions.IgnoreCase | RegexOptions.Singleline);

            return key;
        }
    }
}
